package punto1;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Punto1 {

	public Punto1() throws IOException{
		
		Nodo tope1=null;
		Nodo tope2=null;
		Nodo tope3=null;
		Nodo tope4=null;
		Nodo tope5=null;
		
		tope1=ingresar(tope1,1);
		tope2=ingresar(tope2,2);
		imprimir(tope1,"1");
		imprimir(tope2,"2");
		tope3=invertir(tope1);
		tope4=invertir(tope2);
		tope5=convinar(tope3,tope4,tope5);
		imprimir(tope5,"Final>>>>>>>");
	}
	
	private Nodo convinar(Nodo tope1, Nodo tope2, Nodo top) {
		Nodo tope = new Nodo();
		Nodo pila;
		tope=null;
		Nodo aux[] = new Nodo[2];
		aux[0]=tope1;
		aux[1]=tope2;
		
		do{
			for(int i=0;i<2;i++){
				 pila = new Nodo();
				if(tope==null)
					tope=pila;
				else
					pila.anterior=tope;
				
				if( aux[i]!=null){
					pila.info=aux[i].info;
					aux[i]=aux[i].anterior;
					tope=pila;
				}
				
			}		
			
			
		}while(aux[0] != null || aux[1] != null);
		return tope;
	}
	
	
	
	/*private Nodo convinar(Nodo tope1, Nodo tope2, Nodo top) {
		Nodo tope = new Nodo();
		tope=null;
		Nodo aux1,aux2;
		aux1=tope1;
		aux2=tope2;
		
		do{
			Nodo pila = new Nodo();
			if(tope==null)
				tope=pila;
			else
				pila.anterior=tope;
			
			if(aux1!=null){
				pila.info=aux1.info;
				aux1=aux1.anterior;
			}
			tope=pila;
			
			pila = new Nodo();
			if(tope==null)
				tope=pila;
			else
				pila.anterior=tope;
			
			if(aux2!=null){
				pila.info=aux2.info;
				aux2=aux2.anterior;
			}
			tope=pila;
			
		}while(aux1 != null || aux2 != null);
		return tope;
	}*/

	private Nodo invertir(Nodo top) {
		Nodo tope = new Nodo();
		tope=null;
		Nodo aux = new Nodo();
		aux=top;
		
		do{
			
			Nodo pila = new Nodo();
			if(tope==null)
				tope=pila;
				else
					pila.anterior=tope;
			
			pila.info=aux.info;
			tope=pila;
		
			aux=aux.anterior;
		}while(aux!=null);
			
		return tope;
	}

	

	private void imprimir(Nodo top, String text) {
		Nodo aux = new Nodo();
		aux=top;
		System.out.print("\n pila:"+text);
		do{
			System.out.print("|"+aux.info);
			aux=aux.anterior;
		}while(aux!=null);
		
	}

	private Nodo ingresar(Nodo tope, int i) throws IOException {
		String dato;
		BufferedReader leer = new BufferedReader(new InputStreamReader(System.in));
		
			System.out.println("llenando pila "+i);
			System.out.println("digite  s para salir");
			
			
			do{
				Nodo pila = new Nodo();
				System.out.println("digite dato :");
				dato=leer.readLine();
				
				if((!dato.equals("s"))){
					
				if(tope==null)
					tope=pila;
					else
						pila.anterior=tope;
				
				pila.info=dato;
				tope=pila;
				}
			}while(!dato.equals("s"));
			
		
		
		return tope;
				
		
	}
	
}
